<?php
/** Cms_Resource_Expense_Item */
if (! class_exists ( 'Cms_Resource_Expense_Item' )) {
	require_once dirname ( __FILE__ ) . '/Expense/Item.php';
}

/**
 * Cms_Resource_Expense
 *
 * @category Cms
 * @package Cms_Model_Resource
 */
class Cms_Resource_Expense extends BIS_Model_Resource_Db_Table_Abstract implements Cms_Resource_Expense_Interface {
	protected $_name = 'expense';
	protected $_primary = 'entryId';
	protected $_rowClass = 'Cms_Resource_Expense_Item';
	
	public function getExpenseByEntryId($id) {
		return $this->find ( $id )->current ();
	}
	
	public function getExpenses($data = array()) {
		
		$select = $this->select ();
		
		if (isset ( $data ['order'] )) {
			$select->order ( $data ['order'] );
		}
		
		if (isset($data['search'])) {
			$search = $data['search'];
			$select->where("description like ?", "%$search%");
		}
		
		if (isset ( $data ['page'] )) {
			$adapter = new Zend_Paginator_Adapter_DbTableSelect ( $select );
			$paginator = new Zend_Paginator ( $adapter );
			$paginator->setItemCountPerPage ( $data ['limit'] )->setCurrentPageNumber ( ( int ) $data ['page'] );
			return $paginator;
		}
		
		return $this->fetchAll ( $select );
	
	}
	
	public function getExpenseReport($fromDate=null, $toDate=null){
		
		$select = $this->select ();
		
		if (!empty($fromDate)) {
			$toDate = (!empty($toDate)) ? $toDate : date('Y-m-d');
			$select->where("date(expenseDate) between '$fromDate' and '$toDate'");
		}
		
		$select->order ('expenseDate DESC');
		
		return $this->fetchAll ( $select );
	}

}